/* main.c */
#include "app_inc.h"

#define DEMO_LPIT_MOVEMENT_CHANNEL 0U
#define DEMO_LPIT_BALANCE_CHANNEL  1U

static void NVIC_Configuration(void);
static void LPIT_Configuration(void);

extern void MoveCtrlSetup(void);
extern void MoveCtrlLoop(void);

extern void BalanceSenseSetup(void);
extern void BalanceSenseLoop(void);


int main(void)
{
    BSP_InitSystem();
    NVIC_Configuration(); /* Enable interrupt for modules. */

    BSP_InitStdioLPUART(115200U);
    BSP_InitGPIO_LED();
    /* Initialize hardware for motor. */
    BSP_InitMotorTurn();
    BSP_InitMotorQuadDecoder();

    printf("\r\n\r\nMotorSpeedPID Example.\r\n");
    printf("Compiled on %s, %s\r\n\r\n", __DATE__, __TIME__);

    /* Initialize the LED. */
    GPIO_SetPinDir(BSP_GPIO_LED1_PORT, BSP_GPIO_LED1_PIN, true); /* LED1. */
    GPIO_SetPinLogic(BSP_GPIO_LED1_PORT, BSP_GPIO_LED1_PIN, true);
    GPIO_SetPinDir(BSP_GPIO_LED2_PORT, BSP_GPIO_LED2_PIN, true); /* LED2. */
    GPIO_SetPinLogic(BSP_GPIO_LED2_PORT, BSP_GPIO_LED2_PIN, false);

    LPIT_Configuration();

    Motor_InitDevice();
    Motor_EnableTurn(true);

    printf("Motor initialization done.\r\n");

    

    MoveCtrlSetup();
    BalanceSenseSetup(); /* 准备平衡控制 */


    LPIT_HAL_StartChannel(LPIT0, DEMO_LPIT_MOVEMENT_CHANNEL);
    LPIT_HAL_StartChannel(LPIT0, DEMO_LPIT_BALANCE_CHANNEL);
    while (1)
    {
        
    }
}

static void NVIC_Configuration(void)
{
    NVIC_EnableIRQ(LPIT0_IRQn);
}

static void LPIT_Configuration(void)
{
    LPIT_ChannelConfig_T lpitChannelConfigStruct;

    PCC->CLKCFG[PCC_LPIT0_INDEX] = PCC_CLKCFG_CGC_MASK | PCC_CLKCFG_PCS(6U); /* PLL. */
    /* LPIT. */
    LPIT_HAL_Enable(LPIT0, true);
    LPIT_HAL_Reset(LPIT0);
    LPIT_HAL_GetChannelConfigForNormalTimer(&lpitChannelConfigStruct);

    /* 配置触发调整小车行进的周期定时器 */
    LPIT_HAL_ConfigChannel(LPIT0, DEMO_LPIT_MOVEMENT_CHANNEL, &lpitChannelConfigStruct);
    LPTI_HAL_SetChannelTimeoutValue(LPIT0, DEMO_LPIT_MOVEMENT_CHANNEL, 75000 * 20); /* BSP_CLK_SPLLDIV2_HZ = 75MHz, 20ms */
    LPIT_HAL_EnableChannelInterrupt(LPIT0, DEMO_LPIT_MOVEMENT_CHANNEL, true);

    /* 配置触发调整小车平衡的周期定时器 */
    LPIT_HAL_ConfigChannel(LPIT0, DEMO_LPIT_BALANCE_CHANNEL, &lpitChannelConfigStruct);
    LPTI_HAL_SetChannelTimeoutValue(LPIT0, DEMO_LPIT_BALANCE_CHANNEL, 75000 * 5); /* BSP_CLK_SPLLDIV2_HZ = 75MHz, 5ms. */
    LPIT_HAL_EnableChannelInterrupt(LPIT0, DEMO_LPIT_BALANCE_CHANNEL, true);
}

/****************************************************************************
 * Interrupts Service.
 ***************************************************************************/
void LPIT0_IRQHandler(void)
{
    if (LPIT_HAL_GetChannelTimeoutFlag(LPIT0, DEMO_LPIT_MOVEMENT_CHANNEL))
    {
        LPTI_HAL_ClearChannelTimeoutFlag(LPIT0, DEMO_LPIT_MOVEMENT_CHANNEL);
        GPIO_TogglePinLogic(BSP_GPIO_LED1_PORT, BSP_GPIO_LED1_PIN);

        /* movement control. */
        MoveCtrlLoop();
    }

    if (LPIT_HAL_GetChannelTimeoutFlag(LPIT0, DEMO_LPIT_BALANCE_CHANNEL))
    {
        LPTI_HAL_ClearChannelTimeoutFlag(LPIT0, DEMO_LPIT_BALANCE_CHANNEL);
        GPIO_TogglePinLogic(BSP_GPIO_LED2_PORT, BSP_GPIO_LED2_PIN);

        /* balance control. */
        BalanceSenseLoop();
    }
}


/* EOF. */

